﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Mask;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using JsalesModel;


namespace JSales
{
    public partial class CustomerForm : Form
    {
        public CustomerForm()
        {
            InitializeComponent();
            InitValidationRules();
        }

        private void InitGrid()
        {
            var dbContext = new JYEntities();
            IQueryable<TbCustomer> customers = from i in dbContext.TbCustomers
                                               select i;
            if (customers.Any())
            {
                cBindingSource.DataSource = customers;
            }
        }

        private void InitEditZone()
        {
            textEditAddress.DataBindings.Clear();
            textEditArea.DataBindings.Clear();
            textEditContact.DataBindings.Clear();
            textEditCustom.DataBindings.Clear();
            textEditFax.DataBindings.Clear();
            textEditID.DataBindings.Clear();
            textEditInvoice.DataBindings.Clear();
            textEditPhone.DataBindings.Clear();
            lookUpEditLevel.DataBindings.Clear();

            textEditAddress.DataBindings.Add("Text", cBindingSource, "Address");
            textEditArea.DataBindings.Add("Text", cBindingSource, "Area");
            textEditContact.DataBindings.Add("Text", cBindingSource, "Contact");
            textEditCustom.DataBindings.Add("Text", cBindingSource, "CustomerInfo");
            textEditFax.DataBindings.Add("Text", cBindingSource, "Fax");
            textEditID.DataBindings.Add("Text", cBindingSource, "CustomerID");
            textEditInvoice.DataBindings.Add("Text", cBindingSource, "InvoiceInfo");
            textEditPhone.DataBindings.Add("Text", cBindingSource, "Phone");
            lookUpEditLevel.DataBindings.Add("EditValue", cBindingSource, "Level");
        }

        private EditMode mode;

        private enum EditMode
        {
            Normal = 0,
            Edit = 1,
            Add = 2
        }

        private BindingSource cBindingSource = new BindingSource();

        private void ChangeEditMode(EditMode editMode)
        {
            switch (editMode)
            {
                case EditMode.Normal:
                    {
                        gridControlCus.Enabled = true;
                        mode = EditMode.Normal;
                        gridViewCus.OptionsBehavior.Editable = false;
                        gridViewCus.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;

                        barButtonSave.Enabled = false;
                        barButtonCancel.Enabled = false;
                        barButtonEdit.Enabled = true;
                        barButtonDelete.Enabled = true;
                        barButtonAdd.Enabled = true;

                        textEditAddress.Properties.ReadOnly = true;
                        textEditArea.Enabled = false;
                        textEditContact.Enabled = false;
                        textEditCustom.Properties.ReadOnly = true;
                        textEditFax.Enabled = false;
                        textEditID.Properties.ReadOnly = true;
                        textEditInvoice.Properties.ReadOnly = true;
                        textEditPhone.Enabled = false;
                        lookUpEditLevel.Enabled = false;

                        dxValidationProvider1.RemoveControlError(textEditAddress);
                        dxValidationProvider1.RemoveControlError(textEditArea);
                        dxValidationProvider1.RemoveControlError(textEditContact);
                        dxValidationProvider1.RemoveControlError(textEditCustom);
                        dxValidationProvider1.RemoveControlError(textEditFax);
                        dxValidationProvider1.RemoveControlError(textEditID);
                        dxValidationProvider1.RemoveControlError(textEditInvoice);
                        dxValidationProvider1.RemoveControlError(textEditPhone);
                        dxValidationProvider1.RemoveControlError(lookUpEditLevel);
                    }
                    break;
                case EditMode.Edit:
                    {
                        gridControlCus.Enabled = true;
                        mode = EditMode.Edit;
                        gridViewCus.OptionsBehavior.Editable = false;
                        gridViewCus.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;

                        barButtonSave.Enabled = true;
                        barButtonCancel.Enabled = true;
                        barButtonEdit.Enabled = false;
                        barButtonDelete.Enabled = false;
                        barButtonAdd.Enabled = false;

                        textEditAddress.Properties.ReadOnly=false;
                        textEditArea.Enabled = true;
                        textEditContact.Enabled = true;
                        textEditCustom.Properties.ReadOnly=false;
                        textEditFax.Enabled = true;
                        textEditID.Properties.ReadOnly = true;
                        textEditInvoice.Properties.ReadOnly = false;
                        textEditPhone.Enabled = true;
                        lookUpEditLevel.Enabled = true;

                        dxValidationProvider1.Validate();
                    }
                    break;
                case EditMode.Add:
                    {
                        gridControlCus.Enabled=false;
                        textEditAddress.DataBindings.Clear();
                        textEditArea.DataBindings.Clear();
                        textEditContact.DataBindings.Clear();
                        textEditCustom.DataBindings.Clear();
                        textEditFax.DataBindings.Clear();
                        textEditID.DataBindings.Clear();
                        textEditInvoice.DataBindings.Clear();
                        textEditPhone.DataBindings.Clear();
                        lookUpEditLevel.DataBindings.Clear();

                        mode = EditMode.Add;
                        gridViewCus.OptionsBehavior.Editable = false;
                        gridViewCus.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;

                        barButtonSave.Enabled = true;
                        barButtonCancel.Enabled = true;
                        barButtonEdit.Enabled = false;
                        barButtonDelete.Enabled = false;
                        barButtonAdd.Enabled = false;

                        textEditAddress.Properties.ReadOnly = false;
                        textEditArea.Enabled = true;
                        textEditContact.Enabled = true;
                        textEditCustom.Properties.ReadOnly = false;
                        textEditFax.Enabled = true;
                        textEditID.Properties.ReadOnly = true;
                        textEditInvoice.Properties.ReadOnly = false;
                        textEditPhone.Enabled = true;
                        lookUpEditLevel.Enabled = true;

                        textEditAddress.Text = null;
                        textEditArea.Text = null;
                        textEditContact.Text = null;
                        textEditCustom.Text = null;
                        textEditFax.Text = null;
                        textEditID.Text = null;
                        textEditInvoice.Text = null;
                        textEditPhone.Text = null;
                        lookUpEditLevel.EditValue = null;
                        
                        dxValidationProvider1.Validate();
                    }
                    break;
            }
        }

        private void InitValidationRules()
        {
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = "值不能为空！";

            dxValidationProvider1.ValidationMode = ValidationMode.Auto;
            dxValidationProvider1.SetValidationRule(textEditAddress, notEmptyValidationRule);
            dxValidationProvider1.SetValidationRule(textEditArea, notEmptyValidationRule);
            dxValidationProvider1.SetValidationRule(textEditContact, notEmptyValidationRule);
            dxValidationProvider1.SetValidationRule(textEditCustom, notEmptyValidationRule);
            dxValidationProvider1.SetValidationRule(textEditFax, notEmptyValidationRule);
            dxValidationProvider1.SetValidationRule(textEditInvoice, notEmptyValidationRule);
            dxValidationProvider1.SetValidationRule(textEditPhone, notEmptyValidationRule);
            dxValidationProvider1.SetValidationRule(lookUpEditLevel, notEmptyValidationRule);
        }

        private bool IsExistCheck(string customer)
        {
            var dbContext = new JYEntities();
            IQueryable<TbCustomer> customers = from i in dbContext.TbCustomers
                                               where i.CustomerInfo == customer
                                               select i;
            if (customers.Any())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void CustomerForm_Load(object sender, EventArgs e)
        {
            InitGrid();
            InitEditZone();
            ChangeEditMode(EditMode.Normal);

            //汉化界面
            //DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
            //DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active= new DevExpressXtraTreeListLocalizationCHS();

            gridControlCus.DataSource = cBindingSource;
            lookUpEditLevel.Properties.DataSource = GlobalVar.GCusLevels;
            repositoryItemLookUpEditLevel.DataSource = GlobalVar.GCusLevels;
        }

        private void BarButtonAddItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChangeEditMode(EditMode.Add);
        }

        private void barButtonSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (mode == EditMode.Normal)
            {
                return;
            }
            else
            {
                var dbConText = new JYEntities();
                if (mode == EditMode.Add)
                {
                    if (IsExistCheck(textEditCustom.Text))
                    {
                        MessageBox.Show("该客户已存在！");
                    }
                    else
                    {
                        string customerid = "008" + Guid.NewGuid().ToString();
                        if (!lookUpEditLevel.EditValue.Equals(null)
                            && !string.IsNullOrEmpty(textEditAddress.Text)
                            && !string.IsNullOrEmpty(textEditArea.Text)
                            && !string.IsNullOrEmpty(textEditContact.Text)
                            && !string.IsNullOrEmpty(textEditCustom.Text)
                            && !string.IsNullOrEmpty(textEditFax.Text)
                            && !string.IsNullOrEmpty(textEditInvoice.Text)
                            && !string.IsNullOrEmpty(textEditPhone.Text)
                            && !string.IsNullOrEmpty(lookUpEditLevel.Text))
                        {
                            TbCustomer customer = new TbCustomer();

                            customer.CustomerID = customerid;
                            customer.Address = textEditAddress.Text;
                            customer.Area = textEditArea.Text;
                            customer.Contact = textEditContact.Text;
                            customer.CustomerInfo = textEditCustom.Text;
                            customer.Fax = textEditFax.Text;
                            customer.InvoiceInfo = textEditInvoice.Text;
                            customer.Phone = textEditPhone.Text;
                            customer.Level = int.Parse(lookUpEditLevel.EditValue.ToString());

                            dbConText.Add(customer);
                            dbConText.SaveChanges();

                            CustomerForm_Load(sender, e);
                        }
                        else
                        {
                            MessageBox.Show("数据请填写完整！");
                        }
                    }
                }
                else
                {
                    if (!lookUpEditLevel.EditValue.Equals(null)
                        && !string.IsNullOrEmpty(textEditAddress.Text)
                        && !string.IsNullOrEmpty(textEditArea.Text)
                        && !string.IsNullOrEmpty(textEditContact.Text)
                        && !string.IsNullOrEmpty(textEditCustom.Text)
                        && !string.IsNullOrEmpty(textEditFax.Text)
                        && !string.IsNullOrEmpty(textEditInvoice.Text)
                        && !string.IsNullOrEmpty(textEditPhone.Text)
                        && !string.IsNullOrEmpty(lookUpEditLevel.Text))
                    {
                        IQueryable<TbCustomer> customers = from i in dbConText.TbCustomers
                                                           where i.CustomerID == textEditID.Text
                                                           select i;
                        if (customers.Any())
                        {
                            TbCustomer cus = customers.FirstOrDefault();

                            cus.Address = textEditAddress.Text;
                            cus.Area = textEditArea.Text;
                            cus.Contact = textEditContact.Text;
                            cus.CustomerInfo = textEditCustom.Text;
                            cus.Fax = textEditFax.Text;
                            cus.InvoiceInfo = textEditInvoice.Text;
                            cus.Phone = textEditPhone.Text;
                            cus.Level = int.Parse(lookUpEditLevel.EditValue.ToString());

                            dbConText.SaveChanges();

                            CustomerForm_Load(sender, e);
                        }
                    }
                    else
                    {
                        MessageBox.Show("数据请填写完整！");
                    }
                }
            }
        }

        private void barButtonEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChangeEditMode(EditMode.Edit);
        }

        private void barButtonCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ChangeEditMode(EditMode.Normal);
            CustomerForm_Load(sender, e);
        }

        private void barButtonItemSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridViewCus.FindPanelVisible = true;
        }
    }
}